/**
 * @author Patrick
 * Sub-class of PtrGrpType which is a sub-class of Type. 
 * Contains the name and size of the PtrGrpType int. 
 */
public class PointerType extends PtrGrpType{
	/*
	 * Default ctor that creates an PointerType object with
	 * name "pointer" and size of 4 (bytes).
	 */
	private Type baseType;
	
	public PointerType() {
		super("ptr");
	}
	
	public PointerType(String strName) {
		super(strName);
	}
	
	public PointerType(Type baseType) {
		this();
		setBaseType(baseType);
	}
	
	public PointerType(PointerType ptrType) {
		setBaseType(ptrType.getBaseType());
		setSize(ptrType.getSize());
	}
	
	@Override
	public String getName() {
		if (isTypedef()) {
			return super.getName();
		} 
		return printName();
	}
	
	@Override
	public boolean isPointerType(){ 
		return true; 
	}
	
	public Type getBaseType() {
		return baseType;
	}
	public void setBaseType(Type baseType) {
		this.baseType = baseType;
	}
	
	@Override
	public boolean isEquivalentTo(Type t) {
		if(t.isPointerType()) {
			if(getBaseType().isEquivalentTo(((PointerType)t).getBaseType())) {
				return true;
			}
		}
		return false;
	}
	
	@Override
	public boolean isAssignableTo(Type t) {
		return this.isEquivalentTo(t);
	}
	
	public String printName() {
		String s = "*";
		String newString = s;
		Type t = getBaseType();

		if(!t.isPointerType()) {
			newString = t.getName() + s;
		}
		while(t.isPointerType()) {
			s += "*";
			if(!((PointerType)t).getBaseType().isPointerType()) {
				newString = ((PointerType)t).getBaseType().getName() + s; // super.getName()
				break;
			} else {
				t = ((PointerType)t).getBaseType();
			}
		}
		return newString;
	}
	
	public int numOfStars() {
		int count = 1;
		Type t = getBaseType();

		if(!t.isPointerType()) {
			return count;
		}
		while(t.isPointerType()) {
			count++;
			if(!((PointerType)t).getBaseType().isPointerType()) {
				break;
			} else {
				t = ((PointerType)t).getBaseType();
			}
		}
		return count;
	}
}
